// 创建用户相关的小仓库
import { defineStore } from "pinia";
import type { schoolAllResponseData, schoolInfoData, schoolListFormData, schoolListResponseData, schoolResponseData } from "@/api/school/type";
import { reqSchoolAdd, reqSchoolAll, reqSchoolById, reqSchoolDelete, reqSchoolList, reqSchoolUpdate } from "@/api/school";
// 引入state返回对象类型
import type { SchoolState } from "./types/type";
// 创建学校小仓库
let useSchoolStore = defineStore('School',{
    // 这里使用选择式Api写法
    // 存储数据位置
    // 指定state函数返回值类型为SchoolState
    state:() : SchoolState=>{
        return{
            schools:[]
        }
    },
    // 异步|逻辑
    actions:{
        // 获取所有学校
        async getSchoolAll(){
            let result:schoolAllResponseData = await reqSchoolAll();
            if(result.status == 0){
                this.schools = (result.data as schoolInfoData[])
                // 当前async函数返回一个成功的promise
                return "ok"
            }else{
                return Promise.reject(new Error(result.msg))
            }
        },
        // 分页获取学校
        async getSchoolList(data:schoolListFormData){
            let result:schoolListResponseData = await reqSchoolList(data);
            if(result.status == 0){
                this.schools = result.data?.data as schoolInfoData[]
                // 当前async函数返回一个成功的promise
                return result.data
            }else{
                return Promise.reject(new Error(result.msg))
            }
        },
        // 添加学校
        async addSchool(data:schoolInfoData){
            let result:schoolResponseData = await reqSchoolAdd(data);
            if(result.status == 0){
                // 当前async函数返回一个成功的promise
                return "ok"
            }else{
                return Promise.reject(new Error(result.msg))
            }
        },
        // 根据id查询学校
        async getSchoolById(_id:string){
            let result:schoolResponseData = await reqSchoolById(_id);
            if(result.status == 0){
                // 当前async函数返回一个成功的promise
                return result.data
            }else{
                return Promise.reject(new Error(result.msg))
            }
        },
        // 更新学校信息
        async updateSchool(data:schoolInfoData){
            let result:schoolResponseData = await reqSchoolUpdate(data);
            if(result.status == 0){
                // 当前async函数返回一个成功的promise
                return "ok"
            }else{
                return Promise.reject(new Error(result.msg))
            }
        },
        // 删除学校
        async deleteSchool(schoolId:string){
            let result:schoolResponseData = await reqSchoolDelete(schoolId);
            if(result.status == 0){
                // 当前async函数返回一个成功的promise
                return "ok"
            }else{
                return Promise.reject(new Error(result.msg))
            }
        },
    },
    // 计算属性
    getters:{

    }
})
// 对外暴露获取小仓库方法
export default useSchoolStore